package com.freight.tws.controller;

import java.util.Arrays;

import com.freight.common.core.utils.SecurityUtils;
import lombok.RequiredArgsConstructor;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.freight.common.core.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.freight.common.log.annotation.Log;
import com.freight.common.log.enums.BusinessType;
import com.freight.common.core.web.controller.BaseController;
import com.freight.common.core.web.domain.AjaxResult;
import com.freight.common.core.web.page.Query;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.freight.common.core.web.page.Condition;
import com.freight.tws.domain.TwsNotice;
import com.freight.tws.service.ITwsNoticeService;
import com.freight.common.core.web.page.TableDataInfo;

/**
 * 公告消息Controller
 * 
 * @author ruoyi
 * @date 2023-01-24
 */
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/notice")
public class TwsNoticeController extends BaseController {

    private final ITwsNoticeService iTwsNoticeService;

    /**
     * 查询公告消息列表
     */
    @GetMapping("/list")
    public TableDataInfo list(Query query, TwsNotice twsNotice) {
        LambdaQueryWrapper<TwsNotice> lqw = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(twsNotice.getType())) {
            lqw.eq(TwsNotice::getType, twsNotice.getType());
        }
        if (StringUtils.isNotBlank(twsNotice.getContent())) {
            lqw.like(TwsNotice::getContent, twsNotice.getContent());
        }
        if (StringUtils.isNotBlank(twsNotice.getState())) {
            lqw.eq(TwsNotice::getState, twsNotice.getState());
        }
        // 查询发送给自己的公告消息
        lqw.eq(TwsNotice::getNoticeTo, SecurityUtils.getUsername());
        lqw.orderByDesc(TwsNotice::getCreateTime);
        IPage<TwsNotice> list = iTwsNoticeService.page(Condition.getPage(query), lqw);
        return getDataTable(list);
    }

    /**
     * 查询未读消息数量
     */
    @GetMapping("/getUnReadNum")
    public AjaxResult getUnReadNum() {
        LambdaQueryWrapper<TwsNotice> lqw = new LambdaQueryWrapper<>();
        lqw.eq(TwsNotice::getNoticeTo, SecurityUtils.getUsername());
        lqw.eq(TwsNotice::getState, "0");
        int count = iTwsNoticeService.count(lqw);
        return AjaxResult.success(count);
    }

    /**
     * 获取公告消息详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(iTwsNoticeService.getById(id));
    }

    /**
     * 消息已读
     */
    @Log(title = "公告消息", businessType = BusinessType.UPDATE)
    @GetMapping("/read/{id}")
    public AjaxResult read(@PathVariable("id") Long id) {
        return toAjax(iTwsNoticeService.read(id) ? 1 : 0);
    }

    /**
     * 全部已读
     */
    @Log(title = "公告消息", businessType = BusinessType.UPDATE)
    @GetMapping("/reads")
    public AjaxResult reads() {
        return toAjax(iTwsNoticeService.reads() ? 1 : 0);
    }

    /**
     * 删除公告消息
     */
    @Log(title = "公告消息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(iTwsNoticeService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }

}
